<template>
  <div class="todo-footer">
  <label>
    <input type="checkbox" v-model="SelectAll" />
  </label>
  <span> <span>已完成 {{ CompletedQuantity }} </span> / 全部 {{ TodosLength }} </span>
  <button class="btn btn-danger" @click="emit('deeleteTodoAll')">清除已完成任务</button>
</div>
</template>
<script lang="ts">
import { defineComponent,computed, } from "vue";

export default defineComponent({
  name: "Footer",
});
</script>

<script setup lang="ts">
import type { todosType } from "../../App.vue";

  const props = defineProps<{
    todos:todosType
  }>()
  const emit = defineEmits<{
    //是否选中
    (event:'isSelectAll',newType:boolean):void,
    //删除完成的全部任务
    (event:'deeleteTodoAll'):void
  }>()
  //计算全部个数
  const TodosLength = computed(()=>{
    return props.todos.length
  })
  //计算完成的数量
  const CompletedQuantity = computed(()=>{
    return props.todos.reduce((p,c)=>p + (c.comment ? 1 : 0),0)
  })
  //是否全选
  const SelectAll = computed({
    get(){
      /* return (CompletedQuantity.value === props.todos.length && props.todos.length !== 0 )  第一种方法*/
      //第二种方法
      return(
        props.todos.every(item=>item.comment) && props.todos.length !== 0
      )
    },
    set(newType:boolean){
      emit('isSelectAll',newType)
    } 
  })
</script>

<style scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>
